import Strings from '@/i18n';
import { showToast } from '@ray-js/ray';

export const generateCurrentTime = () => {
  const now = new Date();

  const year2 = now
    .getFullYear()
    .toString()
    .slice(0, 2);

  const year4 = now
    .getFullYear()
    .toString()
    .slice(2, 4);

  const year2Str = Number(year2).toString(16);
  const year4Str = Number(year4).toString(16);

  const month = (now.getMonth() + 1).toString(16).padStart(2, '0');
  const day = now
    .getDate()
    .toString(16)
    .padStart(2, '0');
  const hours = now
    .getHours()
    .toString(16)
    .padStart(2, '0');
  const minutes = now
    .getMinutes()
    .toString(16)
    .padStart(2, '0');

  return `${year2Str}${year4Str}${month}${day}${hours}${minutes}`.slice(0, 12);
};

export const decodeTime = (timeOr: string, timeMode: string) => {
  console.log(`🚀 ~ timeMode:`, timeMode);

  const is24 = timeMode === '24h';
  // 生成当前时间的12位编码

  const nowStr = generateCurrentTime();

  const time = timeOr?.length < 12 ? nowStr : timeOr;

  const y1 = parseInt(time.slice(0, 2), 16);

  const y2 = parseInt(time.slice(2, 4), 16);

  const month = parseInt(time.slice(4, 6), 16);

  const d = parseInt(time.slice(6, 8), 16);

  const h = parseInt(time.slice(8, 10), 16);

  const min = parseInt(time.slice(10, 12), 16);

  const monthStr = `${month}`.padStart(2, '0');

  const dayStr = `${d}`.padStart(2, '0');

  const timeStr = new Date(`${y1}${y2}-${monthStr}-${dayStr}`);

  const week = timeStr.getDay();

  const hourStr = is24 ? `${h}` : `${h % 12 === 0 ? 12 : h % 12}`;

  const ampmStr = is24 ? '' : `${h < 12 ? 'AM' : 'PM'}`;

  return {
    year: `${y1}${y2}`,
    month: `${month}`.padStart(2, '0'),
    day: `${d}`.padStart(2, '0'),
    hour: `${h}`,
    minute: `${min}`.padStart(2, '0'),
    week: `${week}`,
    hourStr,
    ampmStr,

    monthStr: Strings.getLang(`month${month}`),
    weekStr: Strings.getLang(`week_${week}`),
    dayStr: Strings.getLang(`day${d}`),
  };
};
